﻿Imports Framework.Data

Public Class DownloadsFiles
    Private Shared mCache As New Generic.List(Of Entities.Downloads.File)
    Shared Sub New()
        Try
            SyncLock mCache
                For Each clsFile As Entities.Downloads.File In DataStore.DownloadsFilesEx.GetAll
                    mCache.Add(clsFile)
                Next
            End SyncLock
        Catch ex As Exception
            Framework.Misc.HandleException(ex)
        End Try
    End Sub

#Region " Events "
    Public Shared Event Updated(ByVal file As Entities.Downloads.File)
    Public Shared Event UpdatedGUI(ByVal file As Entities.Downloads.File)
    Friend Shared Sub OnUpdated(ByVal file As Entities.Downloads.File)
        RaiseEvent Updated(file)
        Framework.Threading.Invoke(AddressOf OnUpdated_Callback, file)
    End Sub
    Private Shared Sub OnUpdated_Callback(ByVal file As Entities.Downloads.File)
        RaiseEvent UpdatedGUI(file)
    End Sub
#End Region

    Public Shared Sub Save(ByVal file As Entities.Downloads.File)
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            Save(file, t)
            t.Complete()
        End Using
    End Sub
    Public Shared Sub Save(ByVal file As Entities.Downloads.File, ByVal transactionScope As TransactionScope)
        SyncLock mCache
            Dim blnIsNew As Boolean = file.Id = 0
            DataStore.DownloadsFilesEx.Save(file, transactionScope)
            If blnIsNew Then mCache.Add(file)
            OnUpdated(file)
        End SyncLock
    End Sub
    Public Shared Sub Delete(ByVal id As Integer)
        Using t As New TransactionScope(TransactionScope.TransactionMode.NullTransaction)
            Delete(id, t)
            t.Complete()
        End Using
    End Sub
    Public Shared Sub Delete(ByVal id As Integer, ByVal transactionScope As TransactionScope)
        SyncLock mCache
            DataStore.DownloadsFilesEx.Delete(id, transactionScope)
            mCache.Remove(GetById(id))
        End SyncLock
    End Sub

    Public Shared Function GetAll() As Generic.List(Of Entities.Downloads.File)
        SyncLock mCache
            Return mCache.ToList
        End SyncLock
    End Function
    Public Shared Function GetAllByItemId(ByVal itemId As Integer) As Generic.List(Of Entities.Downloads.File)
        SyncLock mCache
            Return (From f In mCache Where (f.ItemId = itemId) Select f).ToList
        End SyncLock
    End Function
    Public Shared Function GetAllByItemId(ByVal itemId As Integer, ByVal transactionScope As TransactionScope) As Generic.List(Of Entities.Downloads.File)
        SyncLock mCache
            Return (From f In mCache Where (f.ItemId = itemId) Select f).ToList
        End SyncLock
    End Function
    Public Shared Function GetAllByDirectoryRecursive(ByVal directory As String) As Generic.List(Of Entities.Downloads.File)
        Dim strDir1 As String = directory.ToLower
        Dim strDir2 As String = directory.ToLower & "\"
        SyncLock mCache
            Return (From f In mCache Where (f.Directory.ToLower = strDir1 Or f.Directory.ToLower.StartsWith(strDir2)) Select f).ToList
        End SyncLock
    End Function
    Public Shared Function GetByDirectoryAndFilename(ByVal directory As String, ByVal name As String) As Entities.Downloads.File
        directory = directory.ToLower
        name = name.ToLower
        SyncLock mCache
            Return (From f In mCache Where (f.Directory.ToLower = directory And f.Name.ToLower = name) Select f).FirstOrDefault
        End SyncLock
    End Function
    Public Shared Function GetById(ByVal id As Integer) As Entities.Downloads.File
        SyncLock mCache
            Return (From f In mCache Where (f.Id = id) Select f).FirstOrDefault
        End SyncLock
    End Function
    Public Shared Function GetByNameSizeAndLastChanged(ByVal name As String, ByVal size As Long, ByVal lastChanged As Date) As Entities.Downloads.File
        name = name.ToLower
        SyncLock mCache
            Return (From f In mCache _
                    Where (f.Name.ToLower = name And f.Size = size And f.LastChanged = lastChanged) _
                    Select f).FirstOrDefault
        End SyncLock
    End Function
    Public Shared Function GetAllByStatus(ByVal status As Entities.Downloads.Status) As Generic.List(Of Entities.Downloads.File)
        SyncLock mCache
            Return (From f In mCache _
                    Where (f.Status = status) _
                    Select f).ToList
        End SyncLock
    End Function
    Public Shared Function GetBySHA512HashAndSize(ByVal sha512Hash As String, ByVal size As Long) As Entities.Downloads.File
        SyncLock mCache
            Return (From f In mCache _
                    Where (f.Size = size And f.SHA512Hash = sha512Hash) _
                    Select f).FirstOrDefault
        End SyncLock
    End Function
End Class
